package config.interceptor;/*
package config.interceptor;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import common.Constants;
import exception.ServiceException;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

*/
/**
 * @Author: zl,
 * Date:    2022/7/7 15:55,
 * Version: IntelliJ IDEA 2021.2.1
 * 拦截器 拦截 Token
 *//*

public class JwtInterceptor implements HandlerInterceptor {

    */
/*@Resource
    private UserMapper userMapper;
*//*

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String token = request.getHeader("token"); //从 http 请求头中取出 token
        //如果不是映射/请求到方法直接通过
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        //执行认证
        if(StrUtil.isBlank(token)){
            throw new ServiceException(Constants.CODE_401, "无token，请重新登录");
        }
        //获取 token 中的 user ID
        String userId;
        try{
            userId = JWT.decode(token).getAudience().get(0);
        }catch (JWTDecodeException j){
            throw new ServiceException(Constants.CODE_401, "token验证失败，请重新登录");
        }
        //根据 token 中的 user ID 查询数据库，查看用户存不存在
        UserLoginDTO tokenUser = userMapper.getTokenUser(userId);
        if(null == tokenUser){
            throw new ServiceException(Constants.CODE_401, "用户不存在，请重新登录");
        }else {
            if("1".equals(tokenUser.getDeleteFlag())){
                throw new ServiceException(Constants.CODE_401, "该用户信息不存在！");
            }else if("2".equals(tokenUser.getDeleteFlag())){
                throw new ServiceException(Constants.CODE_401, "该用户已被管理员停用！");
            }
        }
        //用户密码加签验证 token
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(tokenUser.getPassword())).build();
        System.out.println("jwtVerifier:::" + jwtVerifier);
        try{
            jwtVerifier.verify(token); //验证 token
            System.out.println("token:::" + token);
            System.out.println("JWT:::" + jwtVerifier.verify(token));
        }catch (JWTVerificationException e){
            throw new ServiceException(Constants.CODE_401, "token验证失败，请重新登录");
        }
        return true;
    }
}
*/
